In [1]:
# Question 1: Tesla Stock Data with yfinance
import yfinance as yf
# Fetch Tesla data
tesla = yf.Ticker("TSLA")
tesla_data = tesla.history(period="max")
# Reset index and display first 5 rows
tesla_data.reset_index(inplace=True)
tesla_data.head()
Out[1]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 00:00:00-04:00 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0.0 | 0.0 |
| 1 | 2010-06-30 00:00:00-04:00 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0.0 | 0.0 |
| 2 | 2010-07-01 00:00:00-04:00 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0.0 | 0.0 |
| 3 | 2010-07-02 00:00:00-04:00 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0.0 | 0.0 |
| 4 | 2010-07-06 00:00:00-04:00 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0.0 | 0.0 |
In [2]:
# Question 2: Scrape Tesla Revenue Data
import pandas as pd
import requests
from bs4 import BeautifulSoup
from io import StringIO
# Fetch Tesla revenue data
url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
headers = {"User-Agent": "Mozilla/5.0"} # Mimic a browser request
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
# Find the quarterly revenue table
tables = soup.find_all("table")
tesla_revenue = None
for table in tables:
if "Quarterly Revenue" in str(table):
tesla_revenue = pd.read_html(StringIO(str(table)))[0]
tesla_revenue.columns = ["Date", "Revenue"]
# Clean data
tesla_revenue["Revenue"] = tesla_revenue["Revenue"].str.replace("$", "", regex=False).str.replace(",", "", regex=False)
tesla_revenue = tesla_revenue[tesla_revenue["Revenue"] != ""]
tesla_revenue["Revenue"] = pd.to_numeric(tesla_revenue["Revenue"])
break
if tesla_revenue is not None:
print("Last 5 rows of Tesla Revenue Data:")
print(tesla_revenue.tail())
else:
print("Error: Tesla revenue table not found!")
Last 5 rows of Tesla Revenue Data:
Date Revenue
58 2010-06-30 28.0
59 2010-03-31 21.0
60 2009-12-31 NaN
61 2009-09-30 46.0
62 2009-06-30 27.0
In [3]:
# Question 3: GameStop Stock Data with yfinance
import yfinance as yf
# Fetch GameStop stock data
gme = yf.Ticker("GME")
gme_data = gme.history(period="max")
# Reset index and display first 5 rows
gme_data.reset_index(inplace=True)
print(gme_data.head())
Date Open High Low Close Volume \ 0 2002-02-13 00:00:00-05:00 1.620129 1.693350 1.603296 1.691667 76216000 1 2002-02-14 00:00:00-05:00 1.712707 1.716074 1.670626 1.683250 11021600 2 2002-02-15 00:00:00-05:00 1.683250 1.687458 1.658002 1.674834 8389600 3 2002-02-19 00:00:00-05:00 1.666418 1.666418 1.578047 1.607504 7410400 4 2002-02-20 00:00:00-05:00 1.615920 1.662209 1.603296 1.662209 6892800 Dividends Stock Splits 0 0.0 0.0 1 0.0 0.0 2 0.0 0.0 3 0.0 0.0 4 0.0 0.0
In [4]:
# Question 4: Scrape GameStop Revenue Data
import pandas as pd
import requests
from bs4 import BeautifulSoup
from io import StringIO
# Fetch GameStop revenue data
url = "https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue"
headers = {"User-Agent": "Mozilla/5.0"} # Mimic a browser request
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
# Find the quarterly revenue table
tables = soup.find_all("table")
gme_revenue = None
for table in tables:
if "Quarterly Revenue" in str(table):
gme_revenue = pd.read_html(StringIO(str(table)))[0]
gme_revenue.columns = ["Date", "Revenue"]
# Clean data
gme_revenue["Revenue"] = gme_revenue["Revenue"].str.replace("$", "", regex=False).str.replace(",", "", regex=False)
gme_revenue = gme_revenue[gme_revenue["Revenue"] != ""]
gme_revenue["Revenue"] = pd.to_numeric(gme_revenue["Revenue"])
break
if gme_revenue is not None:
print("Last 5 rows of GameStop Revenue Data:")
print(gme_revenue.tail())
else:
print("Error: GameStop revenue table not found!")
Last 5 rows of GameStop Revenue Data:
Date Revenue
60 2010-01-31 3524
61 2009-10-31 1835
62 2009-07-31 1739
63 2009-04-30 1981
64 2009-01-31 3492
In [5]:
import plotly.graph_objects as go
# Define the make_graph function (if not already defined)
def make_graph(stock_data, title):
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
fig = go.Figure()
fig.add_trace(go.Scatter(x=stock_data['Date'], y=stock_data['Close'], name="Stock Price"))
fig.update_layout(title=title, xaxis_title="Date", yaxis_title="Stock Price (USD)")
fig.show()
# Generate the Tesla stock graph
print("\nGenerating Tesla Stock Graph...")
make_graph(tesla_data, "Tesla Stock Price History")
Generating Tesla Stock Graph...
In [6]:
def make_graph(stock_data, title):
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
fig = go.Figure()
fig.add_trace(go.Scatter(x=stock_data['Date'], y=stock_data['Close'], name="Stock Price"))
fig.update_layout(title=title, xaxis_title="Date", yaxis_title="Stock Price (USD)")
fig.show()
# Generate the GameStop stock graph
print("\nGenerating GameStop Stock Graph...")
make_graph(gme_data, "GameStop Stock Price History")
Generating GameStop Stock Graph...